package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;
@Slf4j
@RestController
public class RabbitController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendmq")
    public String rabbitMq(@RequestParam(value = "num",defaultValue = "10") Integer num){
        OrderReturnReasonEntity orderReturnReasonEntity = new OrderReturnReasonEntity();

        //发送的对象消息转为JSON
        OrderEntity orderEntity = new OrderEntity();

        for (int i = 0; i < num; i++) {

            if(i%2==0){

                orderReturnReasonEntity.setId(1l);
                orderReturnReasonEntity.setCreateTime(new Date());
                orderReturnReasonEntity.setName("哈哈"+i);

                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",orderReturnReasonEntity,new CorrelationData(UUID.randomUUID().toString()));
            }else {

                orderEntity.setOrderSn(UUID.randomUUID().toString());

                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",orderEntity);

            }


        }


        return "ok";

    }
}
